// 自定义后台管理系统的JavaScript增强脚本

(function($) {
    'use strict';
    
    // 页面加载完成后执行
    $(document).ready(function() {
        // 美化表格行，增加悬停效果
        $('#changelist table tbody tr').hover(
            function() {
                $(this).addClass('row-highlight');
            },
            function() {
                $(this).removeClass('row-highlight');
            }
        );
        
        // 为添加行按钮添加动画效果
        $('.add-row').click(function() {
            $(this).addClass('btn-pulse');
            setTimeout(function() {
                $('.add-row').removeClass('btn-pulse');
            }, 500);
        });
        
        // 增强过滤器交互
        $('#changelist-filter h2').click(function() {
            $(this).next('ul').slideToggle('fast');
        });
        
        // 添加快速操作按钮的确认提示
        $('.actions select').change(function() {
            if ($(this).val() === 'delete_selected') {
                $('.button.action').click(function() {
                    return confirm('您确定要删除所选项目吗？此操作无法撤销。');
                });
            }
        });
        
        // 为表单字段添加聚焦效果
        $('.form-row input, .form-row select, .form-row textarea').focus(function() {
            $(this).closest('.form-row').addClass('focused-row');
        }).blur(function() {
            $(this).closest('.form-row').removeClass('focused-row');
        });
        
        // 为必填字段标签添加视觉提示
        $('.required label').append(' <span class="required-marker">*</span>');
        
        // 自定义表单字段验证增强
        $('form').submit(function() {
            var hasError = false;
            $('.required input, .required select, .required textarea').each(function() {
                if (!$(this).val()) {
                    $(this).addClass('field-error');
                    hasError = true;
                } else {
                    $(this).removeClass('field-error');
                }
            });
            
            if (hasError) {
                $('<div class="errornote">请修正表单中的错误后再提交。</div>').insertBefore($(this));
                $('html, body').animate({
                    scrollTop: $('.errornote').offset().top - 50
                }, 500);
                return false;
            }
            return true;
        });
        
        // 添加返回顶部按钮
        $('body').append('<a href="#" id="back-to-top" title="返回顶部">↑</a>');
        
        $(window).scroll(function() {
            if ($(this).scrollTop() > 200) {
                $('#back-to-top').fadeIn();
            } else {
                $('#back-to-top').fadeOut();
            }
        });
        
        $('#back-to-top').click(function() {
            $('html, body').animate({
                scrollTop: 0
            }, 600);
            return false;
        });
        
        // 增强内联相关对象的展开/折叠功能
        $('.inline-related h3').click(function() {
            $(this).toggleClass('collapsed');
            $(this).next().slideToggle('fast');
        });
        
        // 为日期选择器添加快捷按钮
        if ($('.vDateField').length > 0) {
            var today = new Date();
            var todayStr = today.getFullYear() + '-' + 
                          ('0' + (today.getMonth() + 1)).slice(-2) + '-' + 
                          ('0' + today.getDate()).slice(-2);
            
            $('.vDateField').after('<div class="date-shortcuts">' + 
                                  '<a href="#" data-date="' + todayStr + '">今天</a> | ' + 
                                  '<a href="#" data-date="clear">清空</a>' + 
                                  '</div>');
            
            $('.date-shortcuts a').click(function(e) {
                e.preventDefault();
                var dateValue = $(this).data('date');
                
                if (dateValue === 'clear') {
                    $(this).closest('.form-row').find('.vDateField').val('');
                } else {
                    $(this).closest('.form-row').find('.vDateField').val(dateValue);
                }
            });
        }
        
        // 增强搜索体验
        $('#searchbar').attr('placeholder', '输入关键字进行搜索...');
        
        // 添加自定义CSS类
        $('body').addClass('enhanced-admin');
    });
    
})(django.jQuery);

// 添加自定义样式
document.head.insertAdjacentHTML('beforeend', `
<style>
    .row-highlight { background-color: rgba(26, 115, 232, 0.05) !important; }
    .focused-row { background-color: rgba(52, 168, 83, 0.05); }
    .field-error { border: 1px solid #ea4335 !important; }
    .required-marker { color: #ea4335; }
    .btn-pulse { animation: pulse 0.5s; }
    
    @keyframes pulse {
        0% { transform: scale(1); }
        50% { transform: scale(1.1); }
        100% { transform: scale(1); }
    }
    
    #back-to-top {
        position: fixed;
        bottom: 20px;
        right: 20px;
        background: rgba(26, 115, 232, 0.8);
        color: white;
        width: 40px;
        height: 40px;
        border-radius: 50%;
        text-align: center;
        line-height: 40px;
        font-size: 20px;
        display: none;
        z-index: 1000;
        text-decoration: none;
    }
    
    #back-to-top:hover {
        background: rgba(26, 115, 232, 1);
    }
    
    .date-shortcuts {
        margin-top: 5px;
        font-size: 12px;
    }
    
    .date-shortcuts a {
        text-decoration: none;
        color: #1a73e8;
    }
    
    .inline-related h3.collapsed:after {
        content: " ▼";
    }
    
    .inline-related h3:after {
        content: " ▲";
    }
</style>
`); 